package org.onvivals.app.modules.ums.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.onvivals.app.modules.auth.util.SaTokenUtil;
import org.onvivals.common.web.Result;
import org.onvivals.module.ums.entity.UmsUserBill;
import org.onvivals.module.ums.service.IUmsUserBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Tag(name = "小程序:账单")
@RequestMapping("/bill")
public class UmsUserBillController {

	private final IUmsUserBillService umsUserBillService;
	/**
	 * 获取账单
	 */
	@GetMapping("/list")
	@Operation(summary = "获取账单")
	public Result<Object> get() {
		Long userId = SaTokenUtil.getUserId();
		List<UmsUserBill> userBillList = umsUserBillService.list(Wrappers.<UmsUserBill>lambdaQuery().eq(UmsUserBill::getUid, userId));
		userBillList.forEach(k->{
			//计算剩余时间
			Date vipExpireTime = k.getVipExpireTime();
			long betweenDay = DateUtil.between(new Date(), vipExpireTime, DateUnit.DAY);
			k.setDay(betweenDay);
			if (vipExpireTime.getTime() < new Date().getTime()) {
				k.setExpire(true);
			}else {
				k.setExpire(false);
			}
		});
		return Result.OK("查询成功",userBillList);
	}
}
